package com.kankan.impl;

import com.kankan.service.SmsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.TimeoutUtils;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class SmsServiceImpl implements SmsService {

    @Autowired
    RedisTemplate<String,String> redisTemplate;

    private final  String CHECK_CODE="CHECK_CODE:";

    @Override
    public Boolean sendMsg(String mobile) {
        //模拟调用短信接口，本地调试使用
        //生成4位短信验证码
        try {
            int code = RandomUtils.nextInt(100000, 999999);
            ValueOperations<String, String> stringStringValueOperations = redisTemplate.opsForValue();
            stringStringValueOperations.set(CHECK_CODE+mobile,String.valueOf(code),5,TimeUnit.MINUTES);
        }catch (Exception e){
            log.error("发送验证码失败");
            return false;
        }
        return true;
    }
}
